package com.guo.loading;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.shapes.Shape;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 基于画图实现形状绘制
 */
public class ShapeView extends View {
    private int width , height;
    private int left , right , top ,bottom;
    private Paint shapePaint;
    //当前形状在全部形状中的序号
    private int index = 0;
    private Shape mShape = Shape.values()[index];
    //可以变化的颜色组合，长度需要与Shape长度相同
    private int[] colors = {R.color.purple_200 , R.color.purple_500 , R.color.purple_700};

    public ShapeView(Context context) {
        super(context);
        init();
    }

    public ShapeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ShapeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setShape(Shape mShape) {
        this.mShape = mShape;
    }

    /**
     * 设置形状和颜色顺序切换
     */
    public void changeShape(){
        index++;
        index%=Shape.values().length;
        this.mShape = Shape.values()[index];
        shapePaint.setColor(getResources().getColor(colors[index],null));
        invalidate();
    }

    //测量应当绘制的形状的宽度，高度
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
        height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
        left = getPaddingLeft();
        right = left + width;
        top = getPaddingTop();
        bottom = top + height;
        Log.d("ShapeView" , "width = "+width+",height = "
                +height+",left = "+left+",right = "+right+",top = "+top+",bottom = "+bottom);
    }

    //绘制形状
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (mShape){
            case CIRCLE:
                drawCircle(canvas);
                break;
            case TRIANGLE:
                drawTriangle(canvas);
                break;
            case RECTANGLE:
            default:
                drawRectangle(canvas);
                break;
        }
    }

    //绘制三角形
    private void drawTriangle(Canvas canvas){
        Path path = new Path();
        int topPoint = top + height - (int)(height*Math.sqrt(3)/2);
        path.moveTo(left/2+right/2 , topPoint);
        path.lineTo(right , bottom);
        path.lineTo(left , bottom);
        path.lineTo(left/2+right/2 , topPoint);
        path.close();
        canvas.drawPath(path , shapePaint);
    }

    //绘制正方形
    private void drawRectangle(Canvas canvas){
        int rectWidth = Math.min(width , height) - 20;
        int rectLeft = left/2 + right/2 - rectWidth/2;
        int rectRight = left/2 + right/2 + rectWidth/2;
        int rectTop = top/2 + bottom/2 - rectWidth/2;
        int rectBottom = top/2 + bottom/2 + rectWidth/2;
        RectF rect = new RectF(rectLeft , rectTop ,  rectRight , rectBottom);
        canvas.drawRoundRect(rect,5,5,shapePaint);
    }
    //绘制圆形
    private void drawCircle(Canvas canvas){
        canvas.drawCircle(left/2+right/2 , top/2+bottom/2 ,
                Math.min(width,height)/2 - 5 , shapePaint);
    }

    //初始化画笔
    private void init(){
        shapePaint = new Paint();
        shapePaint.setAntiAlias(true);
        shapePaint.setColor(getResources().getColor(colors[index],null));
        shapePaint.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    //绘制的形状组合
    public enum Shape{
        CIRCLE,
        TRIANGLE,
        RECTANGLE
    }
}
